library(tidyverse)

DATA <- tribble(
  ~FACET                                                                   , ~THERMO                     , ~PE     , ~CILO    ,~CIHI     ,
  "Controls for demographics"                                              , "Rated immigrants 0 to 25" , 0.1642595, 0.1280228, 0.2004962,
  "Controls for demographics"                                              , "Rated immigrants 26 to 49", 0.3335466, 0.2894034, 0.3776898,
  "Controls for demographics"                                              , "Rated immigrants at 50"   , 0.3461875, 0.2992781, 0.3930969,
  "Controls for demographics"                                              , "Rated immigrants 51 to 75", 0.5392925, 0.5118909, 0.5666941,
  "Controls for demographics"                                              , "Rated immigrants 76 to 99", 0.7160031, 0.6939391, 0.7380671,
  "Controls for demographics"                                              , "Rated immigrants at 100"  , 0.7555224, 0.7132236, 0.7978213,
  "Controls for demographics and partisanship"                             , "Rated immigrants 0 to 25" , 0.1247893, 0.0701673, 0.1794113,
  "Controls for demographics and partisanship"                             , "Rated immigrants 26 to 49", 0.4814865, 0.391484 , 0.571489 ,
  "Controls for demographics and partisanship"                             , "Rated immigrants at 50"   , 0.4807571, 0.404154 , 0.5573602,
  "Controls for demographics and partisanship"                             , "Rated immigrants 51 to 75", 0.6076368, 0.5556088, 0.6596648,
  "Controls for demographics and partisanship"                             , "Rated immigrants 76 to 99", 0.6904788, 0.6484907, 0.732467 ,
  "Controls for demographics and partisanship"                             , "Rated immigrants at 100"  , 0.6887802, 0.6110989, 0.7664614,
  "Controls for demographics, partisanship, and ratings about other groups", "Rated immigrants 0 to 25" , 0.1571762, 0.0710083, 0.2433441,
  "Controls for demographics, partisanship, and ratings about other groups", "Rated immigrants 26 to 49", 0.5098678, 0.3748605, 0.6448751,
  "Controls for demographics, partisanship, and ratings about other groups", "Rated immigrants at 50"   , 0.5438392, 0.4334356, 0.6542427,
  "Controls for demographics, partisanship, and ratings about other groups", "Rated immigrants 51 to 75", 0.5673664, 0.4964271, 0.6383058,
  "Controls for demographics, partisanship, and ratings about other groups", "Rated immigrants 76 to 99", 0.7507673, 0.6890084, 0.8125262,
  "Controls for demographics, partisanship, and ratings about other groups", "Rated immigrants at 100"  , 0.8607868, 0.7579789, 0.9635948)

DATA$FACET  <- factor(DATA$FACET , levels = unique(DATA$FACET))
DATA$THERMO <- factor(DATA$THERMO, levels = rev(unique(DATA$THERMO)))

COLOR.COLD <- "indianred2"
COLOR.WARM <- "darkseagreen3"

theme.z <-   theme(
  axis.text.x        = element_blank(),
  axis.text.x.top    = element_text(size = 15, color = "black", hjust = 1  , margin = margin(t = 8,b = 0)),
  axis.text.y        = element_text(size = 15, color = "black", hjust = 1  , margin = margin(l = 8,r = 8)),
  axis.text.y.right  = element_text(size = 15, color = "black", hjust = 0  , margin = margin(l = 8,r = 8)),
  axis.ticks.x       = element_blank(),
  axis.ticks.y       = element_blank(),
  axis.title.x       = element_text(size = 15, color = "black", hjust = 0.5, margin = margin(t = 8,b = 0)),
  axis.title.y       = element_blank(),
  legend.position    = "none",
  panel.background   = element_rect(size = 0.5, color = "black", fill = "gray90", linetype = "solid"),
  panel.border       = element_rect(size = 1.0, color = "black", fill = NA      , linetype = "solid"),
  panel.grid.major.x = element_blank(), 
  panel.grid.major.y = element_blank(),
  panel.grid.minor.x = element_blank(), 
  panel.grid.minor.y = element_blank(),
  panel.spacing.x    = unit(5, "lines"),
  panel.spacing.y    = unit(1, "lines"),
  plot.background    = element_rect(fill = "white"),
  plot.caption       = element_text(size = 12, hjust = 0  , margin = margin(t = 10)),
  plot.margin        = unit(c(t = 10, r = 45, b = 10, l = 0),"pt"),
  plot.subtitle      = element_text(size = 15, hjust = 0.5, margin = margin(b = 10)),
  plot.title         = element_text(size = 17, hjust = 0.5, margin = margin(t = 0, b = 10), face = "bold"),
  strip.background   = element_rect(size = 1.0, color="black", fill="black"),
  strip.text.x       = element_text(color="white", face="bold", size=15, margin = margin(t = 12, b = 12))
)

ggplot(DATA, aes(x = PE, y = THERMO)) +
  facet_wrap(~FACET, ncol = 1,  dir = "v", labeller = label_wrap_gen(width = 27)) +
  geom_rect(data = filter(DATA, FACET == "Controls for demographics"                                                  ), aes(xmin = min(DATA$PE[DATA$FACET == "Controls for demographics"]                                                  ), xmax = DATA$PE[DATA$FACET == "Controls for demographics"                                                   & DATA$THERMO == "Rated immigrants at 50"], ymin = -Inf, ymax = Inf), color = "black", fill = COLOR.COLD, inherit.aes = FALSE) +
  geom_rect(data = filter(DATA, FACET == "Controls for demographics"                                                  ), aes(xmax = max(DATA$PE[DATA$FACET == "Controls for demographics"]                                                  ), xmin = DATA$PE[DATA$FACET == "Controls for demographics"                                                   & DATA$THERMO == "Rated immigrants at 50"], ymin = -Inf, ymax = Inf), color = "black", fill = COLOR.WARM, inherit.aes = FALSE) +
  geom_rect(data = filter(DATA, FACET == "Controls for demographics and partisanship"                  ), aes(xmin = min(DATA$PE[DATA$FACET == "Controls for demographics and partisanship"]                  ), xmax = DATA$PE[DATA$FACET == "Controls for demographics and partisanship"                          & DATA$THERMO == "Rated immigrants at 50"], ymin = -Inf, ymax = Inf), color = "black", fill = COLOR.COLD, inherit.aes = FALSE) +
  geom_rect(data = filter(DATA, FACET == "Controls for demographics and partisanship"                  ), aes(xmax = max(DATA$PE[DATA$FACET == "Controls for demographics and partisanship"]                  ), xmin = DATA$PE[DATA$FACET == "Controls for demographics and partisanship"                          & DATA$THERMO == "Rated immigrants at 50"], ymin = -Inf, ymax = Inf), color = "black", fill = COLOR.WARM, inherit.aes = FALSE) +
  geom_rect(data = filter(DATA, FACET == "Controls for demographics, partisanship, and ratings about other groups"), aes(xmin = min(DATA$PE[DATA$FACET == "Controls for demographics, partisanship, and ratings about other groups"]), xmax = DATA$PE[DATA$FACET == "Controls for demographics, partisanship, and ratings about other groups" & DATA$THERMO == "Rated immigrants at 50"], ymin = -Inf, ymax = Inf), color = "black", fill = COLOR.COLD, inherit.aes = FALSE) +
  geom_rect(data = filter(DATA, FACET == "Controls for demographics, partisanship, and ratings about other groups"), aes(xmax = max(DATA$PE[DATA$FACET == "Controls for demographics, partisanship, and ratings about other groups"]), xmin = DATA$PE[DATA$FACET == "Controls for demographics, partisanship, and ratings about other groups" & DATA$THERMO == "Rated immigrants at 50"], ymin = -Inf, ymax = Inf), color = "black", fill = COLOR.WARM, inherit.aes = FALSE) +
  geom_errorbarh(aes(xmin = CILO, xmax = CIHI), height = 0, size = 0.5) +
  geom_point(size = 4) +
  geom_text(x = 1.05, y = DATA$THERMO, size = 5.5, hjust = 0, label = format(round(DATA$PE, 2), nsmall = 2)) +
  coord_cartesian(clip = "off") +
  scale_x_continuous(limits = c(0,1), expand = c(0,0)) +
  labs(x = "Predicted probability of a vote\nfor Joe Biden over Donald Trump") +
  theme.z

ggsave(file = "Figure 3.eps", width = 6.5, height = 9.5)
ggsave(file = "Figure 3.svg", width = 6.5, height = 9.5)
